package com.ruoyi.app.config.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.service.user.model.UserInfoModel;

/**
 * 获取当前线程变量中的 用户id、用户名称、Token等信息 注意： 必须在网关通过请求头的方法传入，同时在HeaderInterceptor拦截器设置值。 否则这里无法获取
 *
 * @author ruoyi
 */
public class UserInfoContextHolder {

  private static final TransmittableThreadLocal<UserInfoModel> THREAD_LOCAL = new TransmittableThreadLocal<>();

  public static void set(UserInfoModel userInfoModel) {
    THREAD_LOCAL.set(userInfoModel);
  }

  public static UserInfoModel getUserInfo() {
    return CommonUtils.requireNotNull(THREAD_LOCAL.get(), "无登录信息");
  }

  public static UserInfoModel getUserInfoMayBeNull() {
    return THREAD_LOCAL.get();
  }

  public static void remove() {
    THREAD_LOCAL.remove();
  }
}
